package phone.qyg.com.qyg.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.app.adapter.recycler.CommonAdapter;
import com.app.adapter.recycler.MultiItemTypeAdapter;
import com.app.adapter.recycler.RecyclerViewItemDecoration;
import com.app.adapter.recycler.base.ViewHolder;
import com.app.application.QXApplication;
import com.app.utils.CommonUtil;

import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.Base;

/**
 * 作者：lblbh on 2017/6/5 14:00
 * 邮箱：lanbuhan@163.com
 * 功能：搜索 类型
 */
public class PopuType {

    public static PopupWindow popupWindow;
    /**
     * 直接设置集合
     * @param v
     * @param models
     * @param themeModel
     * @param onClickState
     * @return
     */
    public static PopupWindow setPopuThemes(Activity activity, View v, List<Base> models, final Base themeModel, final OnClickState onClickState){
        LayoutInflater inflater = (LayoutInflater) QXApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.popu_type, null);

        final RecyclerView recycler = (RecyclerView) view.findViewById(R.id.recycler);

        popupWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

//        popupWindow.showAsDropDown(v);
        if (android.os.Build.VERSION.SDK_INT >=24) {
            int[] a = new int[2];
            v.getLocationInWindow(a);
            popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.NO_GRAVITY, a[0] , a[1]+v.getHeight());
//            popupWindow.showAtLocation((QXApplicat.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0 , a[1]+v.getHeight());
        } else{
            popupWindow.showAsDropDown(v);
        }
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
		popupWindow.update();
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        LinearLayoutManager girdLayoutManager = new LinearLayoutManager(QXApplication.getContext(), GridLayoutManager.VERTICAL,false);
        recycler.addItemDecoration(new RecyclerViewItemDecoration(
                    RecyclerViewItemDecoration.MODE_HORIZONTAL,
                    QXApplication.getContext().getResources().getColor(R.color.divider),
                    CommonUtil.dip2px(QXApplication.getContext(),1),0,0));
        recycler.setLayoutManager(girdLayoutManager);

        setRecycleDatas(popupWindow,recycler,models,themeModel,onClickState);


        return popupWindow;
    }


    /**
     * 设置数据源
     * @param models
     */
    private static void setRecycleDatas(final PopupWindow popupWindow, RecyclerView recycler, List<Base>  models, final Base themeModel , final OnClickState onClickState){
        if(!CommonUtil.isRequestList(models))
            return;
        final CommonAdapter adapter = new CommonAdapter<Base >(QXApplication.getContext(),R.layout.item_find_type,models) {
            @Override
            protected void convert(ViewHolder holder, Base model, int position) {
                String title = "";

//                    Theme theme = (Theme) model;
//                    title = theme.title;
//                    if(themeModel != null && theme.title.equals(themeModel.title)){
//                        holder.setSelect(R.id.item_city,true);
//                        holder.setTextColorRes(R.id.item_city,R.color.login_fill_btn);
//                    }

                holder.setText(R.id.item_city,model.title);
            }
        };

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if(popupWindow != null && popupWindow.isShowing()){
                    popupWindow.dismiss();
                }
                Object obj = adapter.getData().get(position);
                onClickState.OnItemClick(obj);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        recycler.setAdapter(adapter);
    }


    public interface OnClickState{
        public void OnItemClick(Object model);
    }
}
